var express = require('express');
var router = express.Router();
var sql = require('./../sql');
var Address = require('./../sql/col/address');
var uuid = require('node-uuid');
var { validateToken } = require('./../utils')
/**
 * @api {get} api/address 获取地址列表
 * @apiDescription 获取地址列表
 * @apiGroup address
 * @apiParam { string } userid 用户id
 * @apiSuccessExample { json } Success-Response:
 * res.send({
      code: '200',
      message: '获取地址列表',
      data
    })
 * @apiSampleRequest /api/address
 * @apiVersion 1.0.0
 */
router.get('/', function(req, res, next) {
  const { userid } = req.query
  sql.find(Address, {userid}, { _id: 0}).then(data => {
    res.send({
      code: '200',
      message: '获取地址列表',
      data
    })
  })
});

/**
 * @api {get} api/address/edit addid获取地址列表
 * @apiDescription 获取地址列表
 * @apiGroup address
 * @apiParam { string } addressid 用户地址id
 * @apiSuccessExample { json } Success-Response:
 * res.send({
      code: '200',
      message: '获取地址列表',
      data
    })
 * @apiSampleRequest /api/address/edit
 * @apiVersion 1.0.0
 */
router.get('/edit', function(req, res, next) {
  const { addressid } = req.query
  sql.find(Address, {addressid}, { _id: 0}).then(data => {
    res.send({
      code: '200',
      message: '获取地址列表',
      data
    })
  })
});
/**
 * @api {post} api/address/add 添加地址
 * @apiDescription 添加地址
 * @apiGroup address
 * @apiParam { string } userid 用户id
 * @apiParam { number } isDefault 是不是默认地址
 * @apiParam { string } name 收件人姓名
 * @apiParam { string } tel 收件人手机
 * @apiParam { string } province 收件人省份
 * @apiParam { string } city 收件人城市
 * @apiParam { string } county 收件人区县
 * @apiParam { string } addressDetail 收件人详细地址
 * @apiSuccessExample { json } Success-Response:
 *res.send({
      code: '200',
      message: '插入地址成功'
    });
 * @apiSampleRequest /api/address/add
 * @apiVersion 1.0.0
 */
router.post('/add', function(req, res, next) {
  // 1. 获取地址相关信息
  let { userid, isDefault, name, tel, province, city, county, addressDetail } = req.body
  isDefault *= 1
  // 2.插入数据
  const inserdata = {
    addressid: 'address_' + uuid.v1(),
    userid, isDefault, name, tel, province, city, county, addressDetail
  }

  //***********判断当前插入的是不是默认地址************** */
  if (isDefault === 1) {
    sql.update(Address, { userid }, { $set: { isDefault: 0 }}, 1).then(() => {
      // 3.插入
      sql.insert(Address, inserdata).then(() => {
        res.send({
          code: '200',
          message: '插入地址成功'
        });
      })
    })
  } else {
    sql.insert(Address, inserdata).then(() => {
      res.send({
        code: '200',
        message: '插入地址成功'
      });
    })
  }
  
});

/**
 * @api {post} api/address/update 修改地址
 * @apiDescription 修改地址
 * @apiGroup address
 * @apiParam { string } userid 用户id
 * @apiParam { string } token token
 * @apiParam { number } isDefault 是不是默认地址
 * @apiParam { string } name 收件人姓名
 * @apiParam { string } tel 收件人手机
 * @apiParam { string } province 收件人省份
 * @apiParam { string } city 收件人城市
 * @apiParam { string } county 收件人区县
 * @apiParam { string } addressDetail 收件人详细地址
 * @apiSuccessExample { json } Success-Response:
 *res.send({
      code: '200',
      message: '插入地址成功'
    });
 * @apiSampleRequest /api/address/update
 * @apiVersion 1.0.0
 */
router.post('/update', function(req, res, next) {
  validateToken(req).then(() => {
      // 1. 获取地址相关信息
  let { addressid, userid, isDefault, name, tel, province, city, county, addressDetail } = req.body
  isDefault *= 1

    //***********判断当前插入的是不是默认地址************** */
    if (isDefault === 1) {
      sql.update(Address, { userid }, { $set: { isDefault: 0 }}, 1).then(() => {
        // 3.修改地址
        sql.update(Address, { addressid }, { $set: { name, tel, province, city, county, addressDetail, isDefault }}).then(() => {
          res.send({
            code: '200',
            message: '修改地址成功'
          });
        })
      })
    } else {
      sql.update(Address, { addressid }, { $set: { name, tel, province, city, county, addressDetail, isDefault }}).then(() => {
        res.send({
          code: '200',
          message: '修改地址成功'
        });
      })
    }
    
  }).catch(() => {
    res.send({
      code: '10119',
      message: 'token验证失败'
    })
  })
});

/**
 * @api {post} api/address/delete 删除地址
 * @apiDescription 删除地址
 * @apiGroup address
 * @apiParam { string } addressid 地址id
 * @apiParam { string } token token
 * @apiSuccessExample { json } Success-Response:
 *res.send({
      code: '200',
      message: '删除地址成功'
    });
 * @apiSampleRequest /api/address/delete
 * @apiVersion 1.0.0
 */
router.post('/delete', function(req, res, next) {
  validateToken(req).then(() => {
      // 1. 获取地址相关信息
  let { addressid } = req.body
    sql.delete(Address, { addressid }).then(() => {
      res.send({
        code: '200',
        message: '删除地址成功'
      })
    })
  }).catch(() => {
    res.send({
      code: '10119',
      message: 'token验证失败'
    })
  })
});
module.exports = router;
